﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace UI
{
    public partial class ManterEpisodio : System.Web.UI.Page
    {
        public static Boolean inserir;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void Button26_Click(object sender, EventArgs e)
        {

        }

        protected void btNovo_Click(object sender, EventArgs e)
        {
            inserir = true;
            tbNome.Focus();
            tbID.Text = "";
            tbID.Enabled = false;
            tbNome.Text = "";
            tbNome.Enabled = true;
            tbPreco.Text = "0";
            tbPreco.Enabled = true;
            tbSinopse.Text = "";
            tbSinopse.Enabled = true;
            tbTempo.Text = "";
            tbTempo.Enabled = true;
            ddlPrograma.Enabled = true;
            cbPayperview.Enabled = true;
            btSalvar.Enabled = true;
            btCancelar.Enabled = true;

        }

        protected void btSalvar_Click(object sender, EventArgs e)
        {
            EpisodioBOMetodos ebom = new EpisodioBOMetodos();
            EpisodioBO ebo = new EpisodioBO();
            ProgramaBO pBO = new ProgramaBO();

            ebo.Nome = tbNome.Text;
            ebo.Payperview = cbPayperview.Checked;
            ebo.Preco = Convert.ToDecimal(tbPreco.Text);
            ebo.Sinopse = tbSinopse.Text;
            ebo.Tempo = Convert.ToDateTime(tbTempo.Text);
            ebo.Programa = pBO;

            if (inserir)
                {
                    ebom.Inserir(ebo);
                }
            else
                {
                    ebo.Id = Convert.ToInt32(tbID.Text);
                    ebom.Alterar(ebo);
                }
            Limpar();
        }

        public void Limpar()
        {
            tbID.Text = "";
            tbNome.Text = "";
            tbPreco.Text = "";
            tbSinopse.Text = "";
            tbTempo.Text = "0:00";
            cbPayperview.Checked = false ;
            tbID.Enabled = false;
            tbNome.Enabled = false;
            tbPreco.Enabled = false;
            tbSinopse.Enabled = false;
            tbTempo.Enabled = false;
            ddlPrograma.Enabled = false;
            cbPayperview.Enabled = false;
            btSalvar.Enabled = false;
            btCancelar.Enabled = false;
          
 
        }

        protected void btCancelar_Click(object sender, EventArgs e)
        {
            Limpar();

        }

        protected void lbAtualizar_Click(object sender, EventArgs e)
        {

            tbNome.Focus();
            tbNome.Enabled = true;            
            tbPreco.Enabled = true;            
            tbSinopse.Enabled = true;            
            tbTempo.Enabled = true;
            ddlPrograma.Enabled = true;
            cbPayperview.Enabled = true;
            btSalvar.Enabled = true;
            btCancelar.Enabled = true;

            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            int id = Convert.ToInt32(linha.Cells[0].Text);

            EpisodioBOMetodos ebom = new EpisodioBOMetodos();
            EpisodioBO ebo =  ebom.Consultar(id);

            tbID.Text = Convert.ToString(id);
            tbNome.Text = ebo.Nome;
            tbSinopse.Text = ebo.Sinopse;
            tbPreco.Text = Convert.ToString(ebo.Preco);
            cbPayperview.Checked = ebo.Payperview;
            tbTempo.Text = Convert.ToString(ebo.Tempo);
            //ddlPrograma.SelectedValue = Convert.ToString(ebo.Programa);
        }

        protected void btConsultar_Click(object sender, EventArgs e)
        {

        }

        protected void gdEpisodio_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbRemover_Click(object sender, EventArgs e)
        {

            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            int id = Convert.ToInt32(linha.Cells[0].Text);

            EpisodioBOMetodos ebom = new EpisodioBOMetodos();
            EpisodioBO ebo = ebom.Consultar(id);

            ebom.Remover(ebo);
            
            gdEpisodio.DataBind();
        }

        

        

        
    }
}
